#include "WidgetProperty.h"
#include <WidgetVariantBool.h>
#include <ECommandManager.h>
#include "EControllerQtProperty.h"
#include "WidgetBox/WidgetBox.h"
#include "ui_WidgetProperty.h"

WidgetProperty::WidgetProperty(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::WidgetProperty)
{
    ui->setupUi(this);

    //Test code, can be commented
    ui->widgetBox->setCategoryType(WidgetBox::CategoryType::Button);

    EControllerQtPropertyPtr controller = NewSmartPtr(EControllerQtProperty)();
    controller->setWidgetBox(ui->widgetBox);
    controller->setName("WidgetPropertyModel");
    this->m_controller = controller;

    ECommandManager::instance()->registDelegate(
            MakeDelegate(&EController::handleCommand, *(this->m_controller))
            );
    // ui->widgetBox->addCategory("Testing");

    /*Testing code: show how to add widget to WidgetBox*/
    // WidgetVariantMatrix* subWidget1 = new WidgetVariantMatrix;
    // WidgetVariantBool* subWidget2 = new WidgetVariantBool;
    // WidgetVariantString* subWidget3 = new WidgetVariantString;
    // ui->widgetBox->addWidgetAndCategory(subWidget1, "category=Test1");
    // ui->widgetBox->addWidgetAndCategory(subWidget2, "category=Test2");
    // ui->widgetBox->addWidgetAndCategory(subWidget3, "category=Test2");

    /*testing Tree Widget add a new sub Widget
     *After testing, using tree widget directly is not good as expected
     *The WidgetVariantMatrix will not be show entirely.
     */

    /*QTreeWidget* tree = new QTreeWidget;
    ui->verticalLayout->addWidget(tree);

    QTreeWidgetItem* item1 = new QTreeWidgetItem;
    tree->addTopLevelItem(item1);
    item1->setText(0, "root");

    QTreeWidgetItem* item2 = new QTreeWidgetItem;
    item1->addChild(item2);
    WidgetVariantVec3* wVec3 = new WidgetVariantVec3;
    tree->setItemWidget(item2, 0, wVec3);

    wVec3->setAutoFillBackground(true);
    wVec3->setPalette(palette());
    wVec3->setBackgroundRole(backgroundRole());
    wVec3->setStyle(style());  // Set parent widget style
    */
}

WidgetProperty::~WidgetProperty()
{
    delete ui;
}
   
EControllerPtr WidgetProperty::getController()
{
    return this->m_controller;
}

void WidgetProperty::setController(EControllerQtPropertyPtr controller)
{
    ECommandManager::instance()->unregistDelegate(this->m_controller.get());
    ECommandManager::instance()->registDelegate(
            MakeDelegate(&EController::handleCommand, *(controller))
                );
    controller->setWidgetBox(ui->widgetBox);
    
    this->m_controller = controller;
}
